package com.xinchao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinchao.constant.BusinessConstant;
import com.xinchao.constant.ModuleConstant;
import com.xinchao.constant.PageConstant;
import com.xinchao.dao.AdvertiserMapper;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.Advertiser;
import com.xinchao.model.OperationLog;
import com.xinchao.model.User;
import com.xinchao.model.dto.AdvertiserListDTO;
import com.xinchao.model.dto.AdvertiserSelectDTO;
import com.xinchao.model.page.PageInfo;
import com.xinchao.service.AdvertisementService;
import com.xinchao.service.AdvertiserService;
import com.xinchao.service.OperationLogService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dxy
 * @date 2018/9/18 18:35
 */
@Service(value = "advertiserService")
public class AdvertiserServiceImpl implements AdvertiserService {
	@Autowired
	private AdvertiserMapper advertiserMapper;
	@Autowired
	private OperationLogService operationLogService;
	@Autowired
	private AdvertisementService advertisementService;

	/**
	 * 保存广告主
	 * @param advertiser Advertiser
	 * @param loginUser     User
	 * @param remoteIP      远程ip
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveAdvertiser(Advertiser advertiser, User loginUser, String remoteIP) throws ServiceException {
		if (advertiser == null || loginUser == null || StringUtils.isBlank(remoteIP)) {
			throw new ServiceException("输入参数不能为空");
		}
		advertiser.setStatus(BusinessConstant.STATUS_ENABLE);
		advertiser.setAddTime(System.currentTimeMillis());
		//md5加密密码
		String password = advertiser.getPassword();
		if (StringUtils.isNotBlank(password)) {
			password = DigestUtils.md5Hex(password);
			advertiser.setPassword(password);
		}
		//保存广告主
		advertiserMapper.insertAdvertiser(advertiser);
		//记录操作日志
		OperationLog operationLog = new OperationLog();
		operationLog.setOperationContent("保存广告主，广告主名称为: " + advertiser.getAdvertiserName());
		operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		operationLog.setOperationModule(ModuleConstant.MODULE_ADVERTISEMENT + "-" + ModuleConstant.MODULE_ADVERTISEMENT_LIST);
		operationLog.setRealname(loginUser.getRealname());
		operationLog.setUserName(loginUser.getUserName());
		operationLog.setOperationIp(remoteIP);
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 修改广告主
	 * @param advertiser Advertiser
	 * @param loginUser     User
	 * @param remoteIP      远程ip
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateAdvertiserById(Advertiser advertiser, User loginUser, String remoteIP) throws ServiceException {
		if (advertiser == null || loginUser == null || StringUtils.isBlank(remoteIP)) {
			throw new ServiceException("输入参数不能为空");
		}
		if (advertiser.getAdvertiserId() == null) {
			throw new ServiceException("广告主ID不能为空");
		}
		//md5加密密码
		String password = advertiser.getPassword();
		if (StringUtils.isNotBlank(password)) {
			password = DigestUtils.md5Hex(password);
			advertiser.setPassword(password);
		}
		//修改广告主
		advertiserMapper.updateAdvertiserById(advertiser);
		//记录操作日志
		OperationLog operationLog = new OperationLog();
		operationLog.setOperationContent("修改广告主，广告主名称为: " + advertiser.getAdvertiserName());
		operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		operationLog.setOperationModule(ModuleConstant.MODULE_ADVERTISEMENT + "-" + ModuleConstant.MODULE_ADVERTISEMENT_LIST);
		operationLog.setRealname(loginUser.getRealname());
		operationLog.setUserName(loginUser.getUserName());
		operationLog.setOperationIp(remoteIP);
		operationLogService.saveOperationLog(operationLog);
	}

	/***
	 * 通过ID删除广告主
	 * @param advertiserId 广告主ID
	 * @param loginUser       User
	 * @param remoteIP        远程ip
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void deleteAdvertiserById(Long advertiserId, User loginUser, String remoteIP) throws ServiceException {
		if (advertiserId == null || loginUser == null || StringUtils.isBlank(remoteIP)) {
			throw new ServiceException("输入参数不能为空");
		}
		Advertiser oldAdvertiser = advertiserMapper.getAdvertiserById(advertiserId);
		if (oldAdvertiser == null) {
			throw new ServiceException("广告主数据不存在，请联系管理员");
		}
		//验证广告主是否被使用
		Long count = advertisementService.countAdvertisementByAdvertiserId(advertiserId);
		if (count > 0) {
			throw new ServiceException("广告主数据被使用，不能删除");
		}
		Advertiser advertiser = new Advertiser();
		advertiser.setAdvertiserId(advertiserId);
		advertiser.setStatus(BusinessConstant.STATUS_DISABLE);
		advertiser.setUpdateTime(System.currentTimeMillis());
		advertiserMapper.updateAdvertiserById(advertiser);
		//记录操作日志
		OperationLog operationLog = new OperationLog();
		operationLog.setOperationContent("删除广告主，广告主名称为: " + oldAdvertiser.getAdvertiserName());
		operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		operationLog.setOperationModule(ModuleConstant.MODULE_ADVERTISEMENT + "-" + ModuleConstant.MODULE_ADVERTISEMENT_LIST);
		operationLog.setRealname(loginUser.getRealname());
		operationLog.setUserName(loginUser.getUserName());
		operationLog.setOperationIp(remoteIP);
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 获取广告主列表
	 * @param advertiserName   广告主名称
	 * @param pageNum             当前页
	 * @param pageSize            每页条数
	 * @return PageInfo<AdvertiserListDTO>
	 */
	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public PageInfo<AdvertiserListDTO> listAdvertiser(String advertiserName,  Integer pageNum, Integer pageSize) {
		//设置默认值
		if (pageNum == null) {
			pageNum = PageConstant.DEFAULT_PAGE_NUM;
		}
		if (pageSize == null) {
			pageSize = PageConstant.DEFAULT_PAGE_SIZE;
		}
		PageHelper.startPage(pageNum, pageSize);
		Page<AdvertiserListDTO> page = advertiserMapper.listAdvertiser(advertiserName);
		PageInfo<AdvertiserListDTO> pageInfo = new PageInfo<>();
		if (page == null) {
			return pageInfo;
		}
		pageInfo.setPageNum(page.getPageNum());
		pageInfo.setPageSize(page.getPageSize());
		pageInfo.setTotal(page.getTotal());
		pageInfo.setList(page.getResult());
		return pageInfo;
	}

	/**
	 * 获取广告选择列表
	 * @return List<AdvertiserSelectDTO>
	 */
	@Override
	public List<AdvertiserSelectDTO> listAdvertiserSelect() {
		return advertiserMapper.listAdvertiserSelect();
	}

	/**
	 * 通过账号获取广告主
	 * @param userName 账号
	 * @return Advertiser
	 * @throws ServiceException
	 */
	@Override
	public Advertiser getAdvertiserByUserName(String userName) throws ServiceException {
		if (StringUtils.isBlank(userName)) {
			throw new ServiceException("账号不能为空");
		}
		return advertiserMapper.getAdvertiserByUserName(userName);
	}
}
